<!doctype html>
<html class="no-js" lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
      
    <script src="<?= BASE_URI ?>js/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Open Sans">
    <link rel="stylesheet" href="<?= BASE_URI ?>css/app.css" />

    <link rel="shortcut icon" href="<?= BASE_URI ?>img/icons/favicon.ico">
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">

    <script src="<?= BASE_URI ?>js/modernizr.js"></script>
    <script src="<?= BASE_URI ?>js/foundation.min.js"></script>

  </head>

	<body>
        <script type="text/javascript"> var URL = "<?=BASE_URI?>";</script>
        <?php 
        if(!(@$page_name=='adminLog')){
            $this->view->partial('admin-common/header');
        }
         ?>
        <?php $this->view->partial($content); ?>
        <script src="<?=BASE_URI?>js/app.js"></script>

        <script type="text/javascript">
$(document).ready(function(){
    if("<?=$admin_info->delete?>" != "on"){
        $('.btnDelete').hide();
    }
    function editAccount(data,admin_id)
    {
        var params = {};
        params.data = data;
        params.admin_id = admin_id;
        return $.ajax({
            url: URL + 'adminapi/editAccount',
            type: 'post',
            data: params,
            dataType: 'json'
        });
    }
    function deleteCompany(company_id)
    {
        var params = {};
        params.company_id = company_id;
        return $.ajax({
            url: URL + 'admin/delete',
            data: params,
            type: 'post',
            dataType: 'json'
        });   
    }
    function changeStatus(company_id,status)
    {
        var params = {};
        params.company_id = company_id;
        params.status = status;
        return $.ajax({
            url: URL + 'admin/changestatus',
            data: params,
            type: 'post',
            dataType: 'json'
        });
    }
    function changeAccountStatus(admin_id,status)
    {
        var params = {};
        params.admin_id = admin_id;
        params.status = status;
        return $.ajax({
            url: URL + 'admin/changeaccountstatus',
            data: params,
            type: 'post',
            dataType: 'json'
        });
    }
    function saveSuggestEdit($company_id,$cis_id)
    {
        var params = {};
        params.cis = $cis_id;
        params.cid = $company_id;
        return $.ajax({
            url : URL + 'suggestedit/approve',
            type: 'post',
            data: params,
            dataType: 'json'
        });
    }   

    var btnContinuePrev = $('.btnContinue').html();

        $(document).on('click','.btnEditAccount',function(){
            var actions = $(this).attr('actions');
            var aid = $(this).attr('aid');
            actions = actions.split(',');
            
            $('#checkBoxes2 input:checkbox').each(function(i,val){
                var name = val.name;
                var is_checked = false;
                for (var i = 0; i < actions.length ; i++) {
                     if((name == "approve" && actions[i] == "Approve") || 
                        (name == "delete" && actions[i] == "Delete") ||
                        (name == "update" && actions[i] == "Update") ||
                        (name == "modify" && actions[i] == "Modify Account") ||
                        (name == "add" && actions[i] == "Add")
                        ){
                        is_checked = true;
                     }
                };
                if(is_checked){
                    val.checked = true;
                }else{
                    val.checked = false;    
                }
            });
            $('.btnSaveEditAccount').attr('aid',aid);
        });
        $(document).on('click','.btnSaveEditAccount',function(){
            var aid = $(this).attr('aid');
            var data = $('#frmEditAccount').serialize();
            $('.btnSaveEditAccount').html('Processing <img src="<?=BASE_URI?>img/loading2.gif"/>'); 
            editAccount(data,aid).done(function(result){
                if(result.status == true){
                    window.location.reload();   
                    
                }else{  
                    $('.btnSaveEditAccount').html('Save');
                    alert(result.messages);
                }
            });
        });
        $(document).on('click','.btnSuspend',function(){
            var company_id = $(this).attr('cid');
            var action = "suspend_company";
            $('.btnContinue').attr('action',action);
            $('.btnContinue').attr('cid',company_id);
            $('#modal_message').html("Are you sure you want to suspend?");
            $('#alertconfirm').foundation('reveal','open');
        });

        $(document).on('click','.btnApprove',function(){
            var company_id = $(this).attr('cid');
            var action = "approve_company";
            $('.btnContinue').attr('action',action);
            $('.btnContinue').attr('cid',company_id);
            $('#modal_message').html("Are you sure you want to approve?");
            $('#alertconfirm').foundation('reveal','open');
        });

       

        $(document).on('click','.btnDelete',function(){
            var company_id = $(this).attr('cid');
            var action = "delete_company";
            $('.btnContinue').attr('action',action);
            $('.btnContinue').attr('cid',company_id);
            $('#modal_message').html("Are you sure you want to delete?");
            $('#alertconfirm').foundation('reveal','open');
        });

        $(document).on('click','.btnDeactivateAccount',function(){
            var admin_id = $(this).attr('aid');
            var action = "deactivate_account";
            $('.btnContinue2').attr('action',action);
            $('.btnContinue2').attr('aid',admin_id);
            $('#modal_message2').html("Are you sure you want to deactivate this account?");
            $('#activate_deactivate').foundation('reveal','open');
        });
        $(document).on('click','.btnReactivateAccount',function(){
            var admin_id = $(this).attr('aid');
            var action = "reactivate_account";
            $('.btnContinue2').attr('action',action);
            $('.btnContinue2').attr('aid',admin_id);
            $('#modal_message2').html("Are you sure you want to reactivate this account?");
            $('#activate_deactivate').foundation('reveal','open');
        });

         $(document).on('click','.btnApproveSuggest',function(){
            var action = "approve_suggest";

            $('.btnContinueSuggest').attr('cisid',$(this).attr('cisid'));
            $('.btnContinueSuggest').attr('cid',$(this).attr('cid'));
            $('#my_modal_message').html("Are you sure you want to approve?");
            $('#suggest_edit').foundation('reveal','open');
        });
        var click_once;

        $(document).on('click','.btnContinueSuggest',function(){
            var cis = $(this).attr('cisid');
            var cid = $(this).attr('cid');
            var curr = $(this);
            if(click_once == 1) return false;   
            click_once = 1;
            
            curr.html('Processing <img src="<?=BASE_URI?>img/loading2.gif"/>'); 
            saveSuggestEdit(cid,cis).done(function(result){
                   console.log(result);
                   if(result.status == true)
                    {
                        curr.html("Processed Succesfully");
                        setTimeout(function(){
                            $('#suggest_edit').foundation('reveal',"close");
                            window.location.reload();
                        },1000); 
                    }else{
                        alert(result.messages);
                        curr.html('Save');
                    }
            });
            
        });
        $(document).on('click','.btnContinue2',function(){
            var admin_id = $(this).attr('aid');
            var action = $(this).attr('action');
            var status;
            var curr = $(this); 
            if(click_once == 1) return false;   
            click_once = 1;
            
            if(action == "deactivate_account")
            {
                
                status = 0;
            }else if(action == "reactivate_account")
            {
                
                status = 1;
            }


            curr.html('Processing <img src="<?=BASE_URI?>img/loading2.gif"/>'); 
            changeAccountStatus(admin_id,status).done(function(result){
                    if(result.status)
                    {
                        curr.html("Processed Succesfully");
                        setTimeout(function(){
                            curr.html(btnContinuePrev);
                            $('#alertconfirm').foundation('reveal',"close");
                            window.location.reload();
                        },1500);
                    }else{
                        curr.html('Save');
                    }

            });

        });

       
        $(document).on('click','.btnContinue',function(){
            var company_id = $(this).attr('cid');
            var action = $(this).attr('action');
            var status;
            var curr = $(this); 
            if(click_once == 1) return false;   
            click_once = 1;
            
            if(action == "suspend_company")
            {
                status = 2;
            }
            else if(action == "delete_company")
            {
                deleteCompany(company_id).done(function(result){
                     if(result.status)
                        {
                            curr.html("Processed Succesfully");
                            setTimeout(function(){
                                curr.html(btnContinuePrev);
                                $('#alertconfirm').foundation('reveal',"close");
                                window.location.reload();
                            },1500);
                        }
                });
                return false;
            }else if(action == "approve_company")
            {
                status = 1;
            }

            curr.html('Processing <img src="<?=BASE_URI?>img/loading2.gif"/>'); 
        
            changeStatus(company_id,status).done(function(result){
            if(result.status)
            {
                curr.html("Processed Succesfully");
                setTimeout(function(){
                    curr.html(btnContinuePrev);
                    $('#alertconfirm').foundation('reveal',"close");
                    window.location.reload();
                },1500);
            }

            });
        });
    });
    
</script>
	</body>
</html>